{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 作业2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 每行第一字母表示本人，其他字母他的朋友"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "friends = [\n",
    "    ['A', 'B', 'C', 'D', 'E', 'F'],\n",
    "    ['B', 'A', 'C', 'D', 'E'],\n",
    "    ['C', 'A', 'B', 'E'],\n",
    "    ['D', 'A', 'B', 'E'],\n",
    "    ['E', 'A', 'B', 'C', 'D'],\n",
    "    ['F', 'A'],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_map(friends):\n",
    "    friend_content= {}\n",
    "    for friend in friends:\n",
    "        friend_content[friend[0]] = []\n",
    "        for person in friend[1:]:\n",
    "            friend_content[friend[0]].append(person)\n",
    "    return friend_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_reduce(content):\n",
    "    person_content = {}\n",
    "    for person in content:\n",
    "        for i in content[person]:\n",
    "            if not i in person_content:\n",
    "                person_content[i] = 1\n",
    "            else:\n",
    "                person_content[i] = person_content[i] + 1\n",
    "    return person_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 模拟执行map\n",
    "map_result = my_map(friends)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 模拟执行reduce\n",
    "reduce_result = my_reduce(map_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1、问题一：在reduce_result里每一个key的value大于等于3，即代表是有共同朋友的人\n",
    "# 2、这里的\"有共同朋友\"以两者之间是朋友的前提下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def have_same_friends(content):\n",
    "    person_have_same_friends = []\n",
    "    for key in content:\n",
    "        if content[key] >= 3:\n",
    "            person_have_same_friends.append(key)\n",
    "    return person_have_same_friends"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "有共同朋友的人是： ['B', 'C', 'D', 'E', 'A']\n"
     ]
    }
   ],
   "source": [
    "print('有共同朋友的人是：',have_same_friends(reduce_result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1、问题二：共同朋友是谁\n",
    "# 2、每两两人提取出来做一次reduce，value等于2的key即为两者的共同朋友"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def two_people_friends(content):\n",
    "    dict_to_list = list(content)\n",
    "    num = 0\n",
    "    for i in dict_to_list[:-1]:\n",
    "        for j in dict_to_list[num+1:]:\n",
    "            new_dict = {i:content[i],j:content[j]}\n",
    "            result = my_reduce(new_dict)\n",
    "            same_friends = [x for x in result if result[x]==2]\n",
    "            if same_friends:\n",
    "                print('{} 和 {} 的共同朋友有：{}'.format(i, j, same_friends))\n",
    "            else:\n",
    "                print('{} 和 {} 的没有共同朋友'.format(i, j))\n",
    "        num = num + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A 和 B 的共同朋友有：['C', 'D', 'E']\n",
      "A 和 C 的共同朋友有：['B', 'E']\n",
      "A 和 D 的共同朋友有：['B', 'E']\n",
      "A 和 E 的共同朋友有：['B', 'C', 'D']\n",
      "A 和 F 的没有共同朋友\n",
      "B 和 C 的共同朋友有：['A', 'E']\n",
      "B 和 D 的共同朋友有：['A', 'E']\n",
      "B 和 E 的共同朋友有：['A', 'C', 'D']\n",
      "B 和 F 的共同朋友有：['A']\n",
      "C 和 D 的共同朋友有：['A', 'B', 'E']\n",
      "C 和 E 的共同朋友有：['A', 'B']\n",
      "C 和 F 的共同朋友有：['A']\n",
      "D 和 E 的共同朋友有：['A', 'B']\n",
      "D 和 F 的共同朋友有：['A']\n",
      "E 和 F 的共同朋友有：['A']\n"
     ]
    }
   ],
   "source": [
    "two_people_friends(map_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
